package com.cheyun.netsalev3.view.home

import android.util.Log
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.GridLayoutManager
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.databinding.HomeTaskFragment1FragmentBinding
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.viewmodel.HomeTaskFragment1ViewModel
import com.jeremyliao.liveeventbus.LiveEventBus



class HomeTaskFragment1 : BaseFragment<HomeTaskFragment1FragmentBinding>() {
    override fun initContentView(): Int {
        return R.layout.home_task_fragment1_fragment
    }

    override fun initViewModel(): ViewModel? {
        var factory = InjectorUtils.getHomeTaskFragment1ViewModelFactory()
        return ViewModelProviders.of(this, factory).get(HomeTaskFragment1ViewModel::class.java)
    }

    companion object {
        fun newInstance() = HomeTaskFragment1()
    }

    private lateinit var adapter: MyAdapter
    private lateinit var contentAdapter: MyAdapter

    override fun initView() {
        binding.recyclerViewTop.layoutManager = object : GridLayoutManager(context, 4) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        adapter = MyAdapter(R.layout.home_task_top_item, binding.viewModel)
        binding.recyclerViewTop.adapter = adapter
        contentAdapter = MyAdapter(R.layout.home_content_item, binding.viewModel)
        binding.recyclerViewContent.adapter = contentAdapter
//        binding.messageBut.setOnClickListener {
//            startActivity(Intent(context, MessageListActivity::class.java))
//        }
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.viewModel?.refresh()
        }
        binding.viewModel?.networkState?.observe(this, Observer {
            binding.swipeRefreshLayout.isRefreshing = it == NetworkState.LOADING
        })
        binding.recyclerViewTop.addOnOffsetChangedListener {
            binding.swipeRefreshLayout.isRefreshEnabled = it >= -5
        }
    }


    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        Log.e("setUserVisibleHint", isVisibleToUser.toString())
        if (userVisibleHint && isBindingInit()) {
            binding?.viewModel?.refreshMessage()
            binding?.viewModel?.getMoney()
        }
    }

    override fun onResume() {
        super.onResume()
        Log.e("onResume", userVisibleHint.toString())
        if (userVisibleHint && isBindingInit()) {
            binding?.viewModel?.refreshMessage()
            binding?.viewModel?.getMoney()
        }
    }


    override fun initData() {
        binding.viewModel?.listTop?.observe(this, Observer { lists ->
            adapter.submitList(lists)
            adapter.notifyDataSetChanged()
        }
        )
        binding.viewModel?.listContent?.observe(this, Observer { lists ->
            contentAdapter.submitList(lists)
            contentAdapter.notifyDataSetChanged()
        }
        )
        binding.viewModel?.isHuafeiTipShow2?.observe(this, Observer {
            System.out.println(it)
            val para = binding.lyTop.layoutParams
            para.height = resources.getDimension(if(it) R.dimen.home_task_fragment1_huafei_dp else R.dimen.home_task_fragment1_dp).toInt()
            binding.lyTop.setLayoutParams(para)
        })
        //接受通知消息
        LiveEventBus.get()
                .with("notice", String::class.java)
                .observe(this, Observer {
                    binding?.viewModel?.refresh()
                })

        LiveEventBus.get()
                .with("to_audit", String::class.java)
                .observe(this, Observer {
                    var to_audit =  binding?.viewModel?.manageParam?.get()?.to_audit?.toInt()
                    to_audit = to_audit?.minus(1);
                    binding?.viewModel?.manageParam?.get()?.to_audit = to_audit.toString()
                    binding.txtToAudit.text = to_audit.toString();
                })

    }

    override fun lazyInit() {
        Log.i(TAG,"override lazyinit")
    }

}



